<template>
	<view class="phoneCode" v-if="isShow==true">
		<view class="ShowModal">
			<view class="modal">
				<view class="row">
					<view class="bag">
						<image src="https://img.zhizhuangtong.com/project_media/img/xcx/khd/codeBag.png" mode=""
							class="bagImg">
						</image>

						<view class="inneerConter">

							<view class="icon">
								<image src="https://img.zhizhuangtong.com/project_media/img/xcx/khd/codeIcon.png"
									mode="" class="img">
								</image>

								<view class="close" @click="close">
									x
								</view>

							</view>

							<text class="labels">
								为确保预留的手机号可有效联系到您，请将预留手机号收到的验证码填写到下方：
							</text>

							<view class="from">
								<view class="col">
									<text class="label">手机号：</text>
									<text class="val">{{householder_phone}}</text>
								</view>
								<view class="col">
									<text class="label">验证码：</text>
									<view class="right">
										<u--input v-model="code" placeholder="" clearable maxlength="6"></u--input>
										<view class="getPhoneCode" v-if="!getCodeShow" @click="getCode()">获取验证码
										</view>
										<view class="time" v-if="getCodeShow">{{ timer }}s</view>


									</view>
								</view>
							</view>

							<view class="btn">
								<button @click="verification">验证</button>
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow,
	} from '@dcloudio/uni-app'

	import {
		progress
	} from '@/api/warm.js'
	import {
		ref,
		onMounted,
		watch,
		defineEmits
	} from 'vue';
	import {
		string
	} from 'uview-plus/libs/function/test';
	import {
		getCodeRequst,
	} from '@/api/login.js'

	const emit = defineEmits(['saveCde'])
	const householder_phone = ref('')
	// const props = defineProps({
	// 	householder_phone: {
	// 		type: String,
	// 		required: true
	// 	}
	// });

	const isShow = ref(false)
	const code = ref('')

	const isQuer = ref(false)

	let timer = ref(60);
	let t = null;
	let getCodeShow = ref(false);
	//获取验证码
	const getCode = async (phone) => {
		let res = await getCodeRequst({
			phone: householder_phone.value,
			scene: '14',
		})
		uni.showToast({
			icon: 'none',
			title: res.msg
		})
		if (res.code == 1) {
			isQuer.value = true

			getCodeShow.value = true
			if (t) clear();
			t = setInterval(() => {
				timer.value--;
				if (timer.value === 0) clear();
			}, 1000);
		} else {
			isQuer.value = false
			getCodeShow.value = false
		}
		console.log(res.data)
	}

	const clear = () => {
		clearInterval(t);
		timer.value = 60;
		getCodeShow.value = false;
		t = null;
	};



	const showModel = (phone) => {
		clear()
		householder_phone.value = phone
		isShow.value = true
		code.value = ''

	}
	// 关闭
	const close = () => {
		clear()
		isShow.value = false
	}

	const verification = () => {
		console.log('isQuer.value', isQuer.value)
		if (isQuer.value == false) {
			uni.showToast({
				icon: 'none',
				title: '请先发送验证码'
			})
		} else {
			isShow.value = false
			clear()
			isQuer.value = false
			emit('saveCde', code.value)
		}

	}



	// 对外暴露方法
	defineExpose({
		showModel
	});
</script>
<style scoped lang="less">
	.phoneCode {
		width: 100%;
		min-height: 100vh;
		position: fixed;
		z-index: 11;
		left: 0;
		top: 0;
		background-color: rgba(0, 0, 0, 0.3);
		display: flex;

		.ShowModal {
			width: 100%;
			position: relative;
			z-index: 12;

			.modal {
				position: absolute;
				left: 50%;
				bottom: 30%;
				transform: translateX(-50%);
				width: 660rpx;
				z-index: 13;

				.row {
					position: relative;
					display: flex;
					width: 100%;
					height: 50vh;
					min-height: 73 0rpx;

					.bag {
						position: absolute;
						left: 0;
						top: 0;
						width: 100%;
						height: 100%;
						z-index: 14;


						.bagImg {
							width: 100%;
							height: 100%;
							border-radius: 30rpx;
							overflow: hidden;

						}

						.inneerConter {
							position: absolute;
							left: 0;
							top: 0;
							width: 100%;
							height: 100%;
							z-index: 15;
							padding: 25rpx 45rpx;
							box-sizing: border-box;

							.icon {
								width: 100%;
								display: flex;
								justify-content: center;
								box-sizing: border-box;

								.img {
									width: 170rpx;
									height: 200rpx;
								}

								.close {
									position: absolute;
									right: 20rpx;
									height: 20rpx;
									width: 40rpx;
									height: 40rpx;
									text-align: center;
									font-size: 40rpx;
									line-height: 40rpx;

								}
							}

							.labels {
								width: 100%;
								display: block;
								text-align: center;
								margin-bottom: 30rpx;

								font-family: PingFangSC, PingFang SC;
								font-weight: 500;
								font-size: 30rpx;
								color: #333333;
								line-height: 50rpx;
								text-align: left;
								font-style: normal;

							}

							.from {
								width: 100%;
								display: flex;
								flex-wrap: wrap;

								.col {
									width: 100%;
									height: 70rpx;
									display: flex;
									align-items: center;
									margin-bottom: 20rpx;

									.label {
										width: 130rpx;
										font-family: PingFangSC, PingFang SC;
										font-weight: 400;
										height: 100%;
										font-size: 28rpx;
										color: #333333;
										display: flex;
										align-items: center;
										text-align: left;
										font-style: normal;
									}

									.right {
										flex: 1;
										height: 100%;
										display: flex;

										.getPhoneCode {
											padding-left: 20rpx;
											display: flex;
											align-items: center;
											width: 150rpx;
											height: 100%;
											font-family: PingFangSC, PingFang SC;
											font-weight: 400;
											font-size: 28rpx;
											color: #333;
											font-style: normal;
										}

										.time {
											padding-left: 20rpx;
											display: flex;
											align-items: center;
											width: 150rpx;
											height: 100%;
											font-family: PingFangSC, PingFang SC;
											font-weight: 400;
											font-size: 28rpx;
											color: #CCA05F;
											font-style: normal;
										}
									}

									.val {
										flex: 1;
									}
								}
							}

							.btn {
								width: 100%;
								display: flex;
								justify-content: center;
								box-sizing: border-box;
								margin-top: 40rpx;

								button {
									width: 100%;
									height: 88rpx;
									background: linear-gradient(138deg, #EDA55B 0%, #D09A47 100%);
									border-radius: 14rpx;
									font-family: PingFangSC, PingFang SC;
									font-weight: 500;
									font-size: 32rpx;
									color: #FFFFFF;
									line-height: 88rpx;
									font-style: normal;
								}

							}

						}
					}
				}


			}
		}
	}
</style>